<?php

namespace app\index\controller;

use think\Controller;

class Member extends Base {
	public function upgrade() {
		$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );
		if ($this->request->isPost ()) {
			$id = $this->request->param ( 'id' );
			$paypwd = $this->request->param ( 'paypwd' );
			if ($paypwd != $info ['paypwd'])
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'paypwd_error' ) ) );
			else {
				$role = model ( 'Member' )->roleInfo ( array ([ 'id','=',$id ] ) );
				if ($info ['money'] < $role ['deposit'])
					echo json_encode ( array ('code' => 0,'msg' => lang ( 'insufficient_balance' ) ) );
				else {
					$data ['role_id'] = $role ['id'];
					$data ['money'] = $info ['money'] - $role ['deposit'];
					model ( 'Member' )->edit ( array ([ 'user_id','=',$info ['user_id'] ] ), $data );
					unset ( $data );
					$data ['user_id'] = $this->uid;
					$data ['role_id'] = $role ['id'];
					$data ['before_role'] = $info ['role_id'];
					$data ['after_role'] = $role ['id'];
					$data ['create_time'] = time ();
					model ( 'Member' )->addUpgrade ( $data );
					unset ( $data );
					$data ['user_id'] = $this->uid;
					$data ['money'] = $role ['deposit'];
					$data ['create_time'] = time ();
					$data ['type'] = 2;
					$data ['before_money'] = $info ['money'];
					$data ['after_money'] = $info ['money'] - $role ['deposit'];
					$data ['remark'] = 'upgrade,' . $role ['title'];
					model ( 'Member' )->addMoneyLog ( $data );
					echo json_encode ( array ('code' => 99,'msg' => lang ( 'upgrade_success' ) ) );
				}
			}
		} else {
			$role = model ( 'Member' )->getAllRole ( array ([ 'status','=',1 ] ) );
			foreach ( $role as $k => $v ) {
				if ($info ['role_id'] == $v ['id'])
					$level = $v ['id'];
			}
			foreach ( $role as $k => $v ) {
				if ($v ['sort_id'] > $level)
					$role [$k] ['disable'] = 0;
				else
					$role [$k] ['disable'] = 0;
			}
			foreach ( $role as $k => $v )
				if ($v ['disable'] == 0) {
					$this->assign ( 'sel_level', $k );
					break;
				}
			$this->assign ( 'info', $info );
			$this->assign ( 'role', $role );
			return $this->fetch ();
		}
	}
	function recode() {
		$data = model ( 'Member' )->getUpgrade ( array ([ 'u.user_id','=',$this->uid ] ) );
		$this->assign ( 'data', $data );
		return $this->fetch ();
	}
}
